package DataStructure.arrayRelevant.arrayApply;

import Algorithm.greedy.EraseOverlapIntervals;
import Common.Constant.C;
import Common.Utils.UTFactory;
import org.junit.Test;

/**
 * @author 蔚蔚樱
 * @version V1.0
 * @date 2022/11/10 21:31
 * @author—Email micromicrohard@outlook.com
 * @blogURL https://blog.csdn.net/Micro_Micro_Hard
 * @description 会议室预约
 * 示例1：有以下会议需要举行：{{1,2},{2,3},{3,4},{4,5},{5,6},{7,8}}
 * {2,4}:标识2点开始，4点结束，问最少需要几个会议室
 * 示例1的答案：1
 * 示例2：有以下会议需要举行：{{1,7},{2,3},{3,6},{5,9},{8,11},{10,11}}
 * 示例2的答案：3
 * 示例3：有以下会议需要举行：{{1,7},{2,3},{8,10},{9,10},{11,17},{15,16}}
 * 示例3的答案：2
 * 相似题目：{@link EraseOverlapIntervals}
 * @blogURL
 */
public class RoomBooking {

    @Test // 验证功能：从数据库获取测试数据，用于单元测试
    public void TestFunc() throws Exception {
        UTFactory.FullTest(this.getClass());
    }

    @Test // 调试功能 : 用于复现错误的测试案例
    public void DoubleTrack() throws Exception {
        String input = "{{1,2},{1,3},{1,4},{2,4}}";
        String output = "3";
        UTFactory.DebugTest(this.getClass(), input, output);
    }

    public int CalMethod(int[][] meetingSchedule) {
        if (meetingSchedule == null || meetingSchedule.length == 0) {
            return 0;
        }
        int max = 0;
        for (int[] schedule : meetingSchedule) {
            if (schedule == null || schedule.length != 2) {
                return C.ErrorNum;
            }
            max = Math.max(schedule[1], max);
        }
        int[] answer = new int[max];
        int bookingRoomNum = 1; // 最少需要一个会议室
        for (int[] schedule : meetingSchedule) {
            // 会议的预定是 左闭右开 区间，{2,3}不会占用 2点和3点 这个时刻
            for (int i = schedule[0]; i < schedule[1]; i++) {
                answer[i]++;
                bookingRoomNum = Math.max(bookingRoomNum, answer[i]);
            }
        }
        return bookingRoomNum;
    }
}
